import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_common_vm.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_intimate_vm.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_liaotian_vm.dart';
import 'package:xindong/routes/tab_chats/xindong_chat_liaotian_yemian.dart';
import 'package:xindong/routes/tab_chats/xindong_chat_intimate_yemian.dart';

class XinDongChatsTabController extends GetxController {
  static XinDongChatsTabController get to => Get.find();

  late int currentIndex = 0;
  late TabController tabController;
  List<XinDongCommonVM> viewClasss = [];

  // Tab
  List<Widget> tabBar() {
    return [const Tab(text: "消息"), const Tab(text: "密友")];
  }

  // TabView
  List<Widget> tabBarViews() {
    return [const XinDongChatConversationYeMian(), const XinDongChatIntimateYeMian()];
  }

  // 初始化
  void initWithVsync(TickerProvider vsync) {
    tabController = TabController(length: tabBar().length, vsync: vsync);
    tabController.addListener(() {
      updateListener();
    });
    viewClasss = [XinDongLiaoTianVM.to, XinDongIntimateVM.to];
  }

  // TabController监听页面切换
  void updateListener() {
    currentIndex = tabController.index.round();
    update();
  }

  // ScrollNotification监听滚动位置切换
  void updateScrollNotification(double offset) {
    int index = (offset + screenWidth / 2).abs() ~/ screenWidth;
    currentIndex = index;
  }

  // 通知刷新
  void notifyRefresh() {
    if (currentIndex < viewClasss.length) {
      viewClasss[currentIndex].notifyRefresh();
    }
  }
}
